package com.talkweb.newproject.admin.sysmenu.service;

import com.talkweb.newproject.admin.sysmenu.dao.SysMenuDao;
import com.talkweb.newproject.admin.sysmenu.dto.SysMenuDTO;
import com.talkweb.newproject.api.model.admin.SysMenuParam;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qsf on 2019/4/18.
 */
@Service
public class SysMenuService implements Serializable {
    private static final long serialVersionUID=1L;

    @Autowired
    private SysMenuDao sysMenuDao;

    /**
     * 新增菜单信息
     * @param sysMenu
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = {Exception.class})
    public Long insertSysMenu(SysMenuDTO sysMenu) throws Exception {
        Map<String,Object> params = new HashMap();
        params.put("dto",sysMenu);
        int count = sysMenuDao.insertSysMenu(params);
        SysMenuDTO dto = (SysMenuDTO) params.get("dto");
        return dto.getId();
    }

    /**
     * 更新菜单信息
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateSysMenu(SysMenuDTO sysMenuDTO) throws Exception {
        Map<String,Object> params = new HashMap();
        params.put("dto",sysMenuDTO);
        sysMenuDao.updateSysMenu(params);
    }

    /**
     * 根据id删除菜单
     */
    @Transactional(rollbackFor = {Exception.class})
    public void deleteSysMenu(String ids) throws Exception {
        Map<String,Object> params = new HashMap();
        params.put("ids",ids);
        sysMenuDao.deleteSysMenu(params);
    }

    /**
     * 根据id查询菜单信息
     */
    public SysMenuDTO querySysMenuByPrimaryKey(String id) throws Exception {
        SysMenuDTO result = sysMenuDao.querySysMenuByPrimaryKey(id);
        if(result== null) result = new SysMenuDTO();
        return result;
    }

    public List<SysMenuDTO> querySysMenuByUserId(String userId) throws Exception {
        return sysMenuDao.querySysMenuByUserId(userId);
    }

    public SysMenuDTO queryLikeSysMenu(SysMenuDTO obj) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("dto",obj);
        return sysMenuDao.queryLikeSysMenu(params);
    }

    public List<SysMenuDTO> searchSysMenu(SysMenuParam obj) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("dto",obj);
        return sysMenuDao.searchSysMenu(params);
    }
}
